The string consists of lowercase Latin letters and spaces. Double all the
vowels in the string, specifically the letters a, e, i, o, u and y.
Input. One line containing only lowercase Latin letters and spaces. The length
of the string does not exceed 1000 characters.
Output. Print the string with all the vowels doubled.
Sampe input |
Sampe output |
welcome to python |
weelcoomee too pyythoon |
strings
Declare a
pointer i at the beginning of the input character array s and a
pointer j at the beginning of the resulting character array t,
initializing them with i = j = 0. For each
character in the string s that is not a vowel, copy s[i] to t[j]
and move the pointer j one position forward. If the character s[i]
is a vowel, add it twice to the end of the string t, and then increment j
by two positions.
Algorithm
implementation
Declare the input character array s and the resulting array t.
char s[1001], t[2001];
Read the input string.
gets(s);
Copy the character s[i] to t[j] and increment the
index j by 1. If s[i] is a vowel, add it twice to the array
t.
int j = 0;
for (int i = 0; i < strlen(s); i++)
if (s[i] == 'a' || s[i] == 'e' || s[i] == 'i' || s[i] == 'o' ||
s[i] == 'u' || s[i] == 'y')
{
t[j++] = s[i];
t[j++] = s[i];
}
else
t[j++] = s[i];
At the end of the resulting string, write a null byte
to indicate its termination.
t[j] = 0;
Print the answer.
puts(t);
Algorithm
implementation – C++
Read the input string.
getline(cin, s);
Initialize an empty string res, that will contain the result.
res = "";
Iterate over the
characters of the string s.
·
If s[i] is a vowel, add two characters s[i]
to res.
·
If s[i] is a consonant, add one characters s[i]
to res.
for (i = 0; i < s.size(); i++)
{
Add the character s[i] to res.
res = res + s[i];
If s[i] is a vowel, add it to res once more.
if (s[i] == 'a' || s[i] == 'e' || s[i] == 'i' || s[i] == 'o' ||
s[i] == 'u' || s[i] == 'y')
res = res + s[i];
}
Print the answer.
cout << res;
Java implementation
import java.util.*;
public class Main
{
public static void
main(String[] args)
{
Scanner con = new
Scanner(System.in);
String s = con.nextLine();
String res = "";
for(int i = 0; i < s.length();
i++)
if (s.charAt(i) == 'a' || s.charAt(i) == 'e' ||
s.charAt(i) == 'i' || s.charAt(i) == 'o' ||
s.charAt(i) == 'u' || s.charAt(i) == 'y' )
res = res + s.charAt(i) + s.charAt(i);
else
res = res + s.charAt(i);
System.out.println(res);
con.close();
}
}
Python implementation
Read the input string.
s = input()
Initialize an empty string res, that will contain the result.
res = ""
Iterate over the
characters of the string s.
·
If ch is a
vowel, add two characters ch to res.
·
If ch is a
consonant, add one characters ch to res.
for ch in
s:
Add the character ch to res.
res += ch
If ch is a vowel, add it to res once more.
if
ch in "aeiouy":
res +=
ch
Print the answer.
print(res)